package com.ganinfo.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.ganinfo.common.Logger;
import com.ganinfo.common.Result;
import com.ganinfo.common.data.TransmitMap;
import com.ganinfo.common.interceptor.LogInterceptor;

/**
 * @ClassName: BaseController
 * @Description: 基础控制器
 * @author: ruikanwang
 * @date: 2017年2月25日 上午9:53:39
 */
public class BaseController {
	
	protected Logger logger = Logger.getLogger(LogInterceptor.class);
	protected HttpServletRequest request;  

    protected HttpServletResponse response;  

    protected HttpSession session;  
    
    /**
	 * 得到PageData
	 */
	public TransmitMap getTransmitMap(){
		return new TransmitMap(this.getRequest());
	}
	
	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}
	
	/**
	 * @Title:       renderParamError
	 * @Description: 渲染失败数据(参数错误)
	 * @param obj
	 * @return
	 * @return:      Result
	 */
    protected Result renderParamError(Object obj) {
        Result result = new Result(403, obj, "参数传递错误", null);
        return result;
    }
	
    /**
     * @Title:       renderError
     * @Description: 渲染失败数据
     * @return
     * @return:      Result
     */
    protected Result renderError() {
        Result result = new Result(500, null, "服务异常", null);
        return result;
    }

    /**
     * @Title:       renderSuccess
     * @Description: 渲染成功数据
     * @param obj 需要返回的对象
     * @return
     * @return:      Result
     */
    protected Result renderSuccess(Object obj) {
    	Result result = new Result(200, obj, "成功", null);
        return result;
    }
    
    /**
     * @Title:       renderOther
     * @Description: 渲染其他数据
     * @param returnCode 返回状态码
     * @param obj 返回数据
     * @param msg 返回提示信息
     * @return
     * @return:      Result
     */
    protected Result renderOther(Integer returnCode,Object obj,String msg) {
    	Result result = new Result(returnCode, obj, msg, null);
        return result;
    }
    
    /**
     * @Title:       logForResult
     * @Description: 输出返回结果日志
     * @return:      void
     */
    public  void logForResult(Result result){
    	logger.info("返回结果"+result.toJson());
    }

}
